#include "stdafx.h"
#include "GraphicalGenerik.hpp"
#include "../graphical_environment/GraphicalMap.hpp"

namespace graphicalactors
{

GraphicalGenerik::GraphicalGenerik( const irr::core::vector3df& _Pos ) :
    m_id(0),
    m_age(0),  
    m_position(_Pos),
    m_radius(20.0f),
    m_entities()
{
    CONNECT( Event::EVENT_CHANGE_POSITION, &GraphicalGenerik::onReceiveChangePosition);
    CONNECT( Event::EVENT_GROW, &GraphicalGenerik::onReceiveGrow);

}

GraphicalGenerik::~GraphicalGenerik()
{
    Event::EventManager::getSingleton().destroyAllConnections( this );

    for ( std::vector< GraphicalEntity * >::iterator it_entity = m_entities.begin();
        it_entity != m_entities.end();
        ++it_entity)
    {
        delete (*it_entity);
    }
    m_entities.clear();
}

void GraphicalGenerik::draw( irr::core::vector3df& _newPosition )
{
    irr::core::vector3df direction(m_position - _newPosition);
    direction.normalize();

    m_position = _newPosition;

    for ( std::vector< GraphicalEntity * >::iterator it_entity = m_entities.begin();
        it_entity != m_entities.end();
        ++it_entity)
    {
        (*it_entity)->setPosition( _newPosition );
        
        _newPosition += direction * m_radius;
    }
}

void GraphicalGenerik::onReceiveChangePosition(boost::shared_ptr< Event::EventBase > _evt)
{
    GET_REAL_EVENT_TYPE( Event::EventChangePosition, evt, _evt );

    if ( evt->getIdChanged() == m_id )
    {
        draw( evt->getNewPosition() );
    }
}

void GraphicalGenerik::onReceiveGrow(boost::shared_ptr< Event::EventBase > _evt)
{
    GET_REAL_EVENT_TYPE( Event::EventGrow, evt, _evt );

    if ( evt->getIdChanged() == m_id )
    {
        grow( evt->getNewAge() );
    }
}

}